package com.bwda.dsrs.filecenter.controller;

import com.bwda.dsrs.filecenter.annotations.IgnoreClientToken;
import com.bwda.dsrs.filecenter.domain.BusinessConstants;
import com.bwda.dsrs.filecenter.domain.condition.datatypewordrel.DataTypeWordRelCondition;
import com.bwda.dsrs.filecenter.domain.po.datatype.DataTypePo;
import com.bwda.dsrs.filecenter.domain.condition.datatype.DataTypeCondition;
import com.bwda.dsrs.filecenter.domain.form.datatype.DataTypeQueryForm;
import com.bwda.dsrs.filecenter.domain.form.datatype.DataTypeUpdateForm;
import com.bwda.dsrs.filecenter.domain.form.datatype.DataTypeCreateForm;
import com.bwda.dsrs.filecenter.domain.po.datatypewordrel.DataTypeWordRelPo;
import com.bwda.dsrs.filecenter.domain.vo.datatype.DataTypeVo;
import com.bwda.dsrs.filecenter.service.DataTypeService;
import com.bwda.dsrs.filecenter.domain.exception.BusinessServiceException;
import com.bwda.dsrs.base.centerbase.response.CentreListResponse;
import com.bwda.dsrs.base.centerbase.response.CentreCutPageResponse;
import com.bwda.dsrs.base.centerbase.response.ResponseEntity;
import java.util.List;
import java.util.ArrayList;
import com.bwda.dsrs.base.util.CopyUtil;
import com.bwda.dsrs.filecenter.service.DataTypeWordRelService;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.apache.commons.lang.math.RandomUtils;

/**
 * @author Administrator
 */
@RestController
@RequestMapping("/b/datatype")
@Api(value = "/b/datatype", tags = "数据类别表")
public class DataTypeController extends BaseController {

	@Autowired
	private DataTypeService dataTypeService;

	@Autowired
	private DataTypeWordRelService dataTypeWordRelService;

	@ApiOperation(value = "查询数据类别表",notes = "查询数据类别表",httpMethod = "GET")
	@RequestMapping(value = "/query", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity<DataTypeVo> query(@ApiParam(value = "type_id", required = true)@RequestParam Integer typeId) throws BusinessServiceException {
		DataTypePo po = dataTypeService.queryWithValid(typeId);
		DataTypeVo vo = CopyUtil.transfer(po, DataTypeVo.class);
		return getSuccessResult(vo);
	}

	@ApiOperation(value = "查询数据类别表数量",notes = "查询数据类别表数量",httpMethod = "GET")
	@RequestMapping(value = "/queryCount", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity<Integer> queryCount(@ModelAttribute@Valid DataTypeQueryForm form) throws BusinessServiceException {
		DataTypeCondition condition = this.getConditionByQueryForm(form);
		int count = dataTypeService.queryCount(condition);
		return getSuccessResult(count);
	}

	@ApiOperation(value = "查询数据类别表列表",notes = "查询数据类别表列表",httpMethod = "GET")
	@RequestMapping(value = "/queryList", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity<CentreListResponse<DataTypeVo>> queryList(@ModelAttribute@Valid DataTypeQueryForm form) throws BusinessServiceException {
		DataTypeCondition condition = this.getConditionByQueryForm(form);
		List<DataTypePo> poList = dataTypeService.queryList(condition);
		List<DataTypeVo> voList = CopyUtil.transfer(poList, DataTypeVo.class);
		return getSuccessResult(getListResponse(voList));
	}

	@ApiOperation(value = "查询数据类别表列表(带分页)",notes = "查询数据类别表列表(带分页)",httpMethod = "GET")
	@RequestMapping(value = "/queryPageList", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity<CentreCutPageResponse<DataTypeVo>> queryPageList(@ModelAttribute@Valid DataTypeQueryForm form) throws BusinessServiceException {
		DataTypeCondition condition = this.getConditionByQueryForm(form);
		condition.setDelFlag(BusinessConstants.NumberConstant.ZERO);
		List<DataTypeVo> voList = new ArrayList<>();
		int count = dataTypeService.queryCount(condition);
		if (count > BusinessConstants.NumberConstant.ZERO) {
			List<DataTypePo> poList = dataTypeService.queryList(condition);
			voList = CopyUtil.transfer(poList, DataTypeVo.class);
		}
		return getSuccessResult(getPageResponse(form, count, voList));
	}

	@IgnoreClientToken
	@ApiOperation(value = "查询数据类别表列表",notes = "查询数据类别表列表",httpMethod = "GET")
	@RequestMapping(value = "/queryTreeList", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity<List<DataTypePo>> queryTreeList(@ModelAttribute@Valid DataTypeQueryForm form) throws BusinessServiceException {
		logger.info("查询数据类别表列表（树状）");
		DataTypeCondition condition = this.getConditionByQueryForm(form);
		condition.setDelFlag(BusinessConstants.NumberConstant.ZERO);
		condition.setTypeLevel(BusinessConstants.NumberConstant.ONE);
		List<DataTypePo> dataTypePoList =dataTypeService.queryTreeList(condition);
		return getSuccessResult(dataTypePoList);
	}

	@ApiOperation(value = "新增数据类别表",notes = "新增数据类别表",httpMethod = "POST")
	@RequestMapping(value = "/add", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity<DataTypeVo> add(@ModelAttribute@Valid DataTypeCreateForm form) throws BusinessServiceException {
		logger.info("新增数据类别表");
		DataTypePo po = CopyUtil.transfer(form, DataTypePo.class);
		if (form.getParentTypeId()!=BusinessConstants.NumberConstant.ZERO){
			DataTypePo dataTypePo =dataTypeService.query(form.getParentTypeId());
			po.setTypeLevel(dataTypePo.getTypeLevel()+1);
		}
		dataTypeService.insert(po);
		DataTypeVo vo = CopyUtil.transfer(po, DataTypeVo.class);
		return getSuccessResult(vo);
	}

	@ApiOperation(value = "修改数据类别表",notes = "修改数据类别表",httpMethod = "POST")
	@RequestMapping(value = "/update", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity update(@ModelAttribute@Valid DataTypeUpdateForm form) throws BusinessServiceException {
		logger.info("修改数据类别表");
		DataTypePo po = CopyUtil.transfer(form, DataTypePo.class);
		dataTypeService.update(po);
		return getSuccessResult();
	}

	@ApiOperation(value = "删除数据类别表",notes = "删除数据类别表",httpMethod = "POST")
	@RequestMapping(value = "/delete", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity delete(@ApiParam(value = "typeId", required = true)@RequestParam Integer typeId) throws BusinessServiceException {
		logger.info("删除数据类别表");
		//判断是否绑定字段
		DataTypeWordRelCondition condition = new  DataTypeWordRelCondition();
		condition.setTypeId(typeId);
		List<DataTypeWordRelPo> list =dataTypeWordRelService.queryList(condition);
		if (list.size()>0){
			getFailResult("有绑定字段无法删除");
		}
		//判断是否有下级
		DataTypeCondition condition1 = new DataTypeCondition();
		condition1.setParentTypeId(typeId);
		List<DataTypePo> list1 =dataTypeService.queryList(condition1);
		if (list1.size()>0){
			getFailResult("有下级无法删除");
		}
		dataTypeService.delete(typeId);
		return getSuccessResult();
	}

	/**
	 * DataTypeQueryForm转换为DataTypeCondition
	 *
	 * @param form
	 * @return
	 */
	private DataTypeCondition getConditionByQueryForm(DataTypeQueryForm form) {
		DataTypeCondition condition = CopyUtil.transfer(form, DataTypeCondition.class);
		return condition;
	}



}